package com.atguigu.auth.controller;


import com.atguigu.auth.service.SysUserService;
import com.atguigu.common.result.Result;
import com.atguigu.common.utils.MD5;
import com.atguigu.model.system.SysUser;
import com.atguigu.vo.system.SysUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2024-01-16
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("admin/system/user")
public class SysUserController {
    private final SysUserService sysUserService;

    public SysUserController(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @ApiOperation("修改用户状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result<Object> updateStatus(@PathVariable long id, @PathVariable Integer status) {
        sysUserService.updateStatus(id, status);
        return Result.ok();
    }

    @ApiOperation(value = "获取用户列表")
    @GetMapping("{page}/{limit}")
    public Result<Object> index(@PathVariable long page, @PathVariable long limit, SysUserQueryVo queryVo) {
        // 页面分页信息
        Page<SysUser> page1 = new Page<>(page, limit);
        // 注入查询条件
        LambdaQueryWrapper<SysUser> queryWrapper = new LambdaQueryWrapper<>();

        String keyword = queryVo.getKeyword();
        String createTimeEnd = queryVo.getCreateTimeEnd();
        String createTimeBegin = queryVo.getCreateTimeBegin();
        if (!StringUtils.isEmpty(keyword)) {
            queryWrapper.like(SysUser::getName, keyword);
        }

        if (!StringUtils.isEmpty(createTimeBegin)) {
            // 查询大于等于查询开始时间
            queryWrapper.ge(SysUser::getCreateTime, createTimeBegin);
        }

        if (!StringUtils.isEmpty(createTimeEnd)) {
            // 查询小于等于结束时间
            queryWrapper.le(SysUser::getCreateTime, createTimeEnd);
        }

        Page<SysUser> page2 = sysUserService.page(page1, queryWrapper);
        return Result.ok(page2);


    }

    @ApiOperation(value = "获取用户")
    @GetMapping("get/{id}")
    public Result<Object> get(@PathVariable long id) {
        SysUser user = sysUserService.getById(id);
        return Result.ok(user);
    }

    @ApiOperation(value = "保存用户")
    @PostMapping("save")
    public Result<Object> save(@RequestBody SysUser user) {
        // 对密码进行加密
        user.setPassword(MD5.encrypt(user.getPassword()));
        sysUserService.save(user);
        return Result.ok();
    }

    @ApiOperation(value = "修改用户")
    @PutMapping("update")
    public Result<Object> update(@RequestBody SysUser user) {
        sysUserService.updateById(user);
        return Result.ok();
    }

    @ApiOperation(value = "删除用户")
    @DeleteMapping("remove/{id}")
    public Result<Object> delete(@PathVariable long id) {
        sysUserService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "批量删除用户")
    @DeleteMapping("batchDelete")
    public Result<Object> batchDelete(@RequestBody List<Long> ids) {
        sysUserService.removeByIds(ids);
        return Result.ok();
    }
}

